package pdm.traj.util.timeslot;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class TimeSlot {
	private HourAndMin start;
	private HourAndMin end;
	
	public TimeSlot(int startHour, int startMin, int endHour, int endMin) {
		this.start = new HourAndMin(startHour, startMin);
		this.end = new HourAndMin(endHour, endMin);
	}
	
	public TimeSlot(HourAndMin start, HourAndMin end) {
		this.start = start;
		this.end = end;
	}
	
	public boolean inSlot(Date d) {
		return (start.AfterEqualMe(d) && end.BeforeMe(d));
	}
	
	public boolean inSlot(String s) {
		HourAndMin h = new HourAndMin(s);
		return (start.AfterEqualMe(h) && end.BeforeMe(h));
	}
	
	public String toString() {
		return start.toString() + " - " + end.toString();
	}
	
	public boolean equals(TimeSlot t) {
		return (this.start.equals(t.start) && this.end.equals(t.end));
	}
 	
	public static List<TimeSlot> getTimeSlotList(List<String> input) {
		List<TimeSlot> result = new ArrayList<TimeSlot>();
		String s;
		String[] ss;
		int h0, m0, h1, m1;
		Iterator<String> it = input.iterator();
		if (it.hasNext()) {
			s = it.next();
			ss = s.split(":");
			h0 = Integer.parseInt(ss[0]);
			m0 = Integer.parseInt(ss[1]);
			h1 = h0;
			m1 = m0;
			while (it.hasNext()) {
				s = it.next();
				ss = s.split(":");
				try {
					h1 = Integer.parseInt(ss[0]);
					m1 = Integer.parseInt(ss[1]);
					TimeSlot ts = new TimeSlot(h0, m0, h1, m1);
					result.add(ts);
				} catch (NumberFormatException nfe) {
					nfe.printStackTrace();
				}
				h0 = h1;
				h0 = m1;
			}
		}
		return result;
	}
	
	/**
	 * get linked data time slot list
	 * @return
	 */
	public static List<TimeSlot> getLinkedDataTimeSlotList() {
		List<TimeSlot> result = new ArrayList<TimeSlot>();
		HourAndMin h0 = HourAndMin.getFromMinCount(0);
		HourAndMin h1 = HourAndMin.getFromMinCount(60);
		for (int i = 0; i < 23; i++) {
			result.add(new TimeSlot(h0, h1));
			h0 = HourAndMin.getNext(h0, 60);
			h1 = HourAndMin.getNext(h1, 60);
		}
		h1 = HourAndMin.getMidNight();
		result.add(new TimeSlot(h0, h1));
		return result;
	}
	
	/**
	 * get built data time slot list
	 * @return
	 */
	public static List<TimeSlot> getBuiltDataTimeSlotList() {
		List<TimeSlot> result = new ArrayList<TimeSlot>();
		HourAndMin h0 = HourAndMin.getFromMinCount(0);
		HourAndMin h1 = HourAndMin.getFromMinCount(15);
		for (int i = 0; i < 4 * 24 - 1; i++) {
			result.add(new TimeSlot(h0, h1));
			h0 = HourAndMin.getNext(h0, 15);
			h1 = HourAndMin.getNext(h1, 15);
		}
		h1 = HourAndMin.getMidNight();
		result.add(new TimeSlot(h0, h1));
		return result;
	}
	
	//******* for test start
	private static void testTimeSlotConstructor() {
		TimeSlot ts = new TimeSlot(1, 0, 3, 1);
		System.out.println(ts);
		System.out.println(ts.inSlot("00:32"));
		System.out.println(ts.inSlot("01:00"));
		System.out.println(ts.inSlot("02:14"));
		System.out.println(ts.inSlot("03:00"));
		System.out.println(ts.inSlot("03:01"));
		System.out.println(ts.inSlot("23:32"));
	}
	
	private static void showTimeSlotList(List<TimeSlot> tsl) {
		Iterator<TimeSlot> it = tsl.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
	
	private static void testGetLinkedDataTimeSlotList() {
		System.out.println("linked:");
		showTimeSlotList(getLinkedDataTimeSlotList());
	}
	
	private static void testGetBuiltDataTimeSlotList() {
		System.out.println("built:");
		showTimeSlotList(getBuiltDataTimeSlotList());
	}
	
	private static void test() {
//		testTimeSlotConstructor();
		testGetLinkedDataTimeSlotList();
		testGetBuiltDataTimeSlotList();
	}
	//******* for test end
	
	public static void main(String[] args) {
		test();
	}
}
